<template>
	<view>
		<dialog-shell ref="shell" title="用户登陆" confirm-text="登陆" @confirm="confirmLogin">
			<image src="../../static/icons/icon.png" class="icon"></image>
			<text class="welcome">WELCOME</text>
			<text class="info-register" @click="clickGoRegister">没有注册账号?请注册</text>
			<view class="login-container">
				<input type="text" placeholder="输入用户名:" placeholder-class="input-holder" :value="usrValue"@input="inputGetUser" />
				<text class="iconfont icon-customer-center ic-user"></text>
			</view>
			<view class="login-container" style="margin-bottom: 20rpx;">
				<input v-if="isPwd" :value="pwdValue" type="password" placeholder="输入密码"
					placeholder-class="input-holder" @input="inputGetPwd" />
				<input v-else :value="pwdValue" type="text" placeholder="输入密码" placeholder-class="input-holder"
					@input="inputGetPwd" />
				<text :class="isPwd ? 'iconfont  icon-icon-test1 ic-pwd-status' :'iconfont icon-icon-test ic-pwd-status'"
					@click="clickSwtich"></text>
				<text class="iconfont icon-key ic-user"></text>
			</view>
		</dialog-shell>
	</view>
</template>
<script>
	export default {
		name: "login-dialog",
		data() {
			return {
				isPwd: true,
				pwdValue: "",
				usrValue: ""

			};
		},
		mounted() {
			let userInfo = uni.getStorageSync("userLoginInfo")
			// console.log(userInfo);
			if (userInfo) {
				this.usrValue = userInfo.username
				this.pwdValut = userInfo.password
			}
		},
		methods: {
			clickGoRegister() {
				uni.navigateTo({
					url: '../../pages/register/register',
					success: res => {
						this.$refs.shell.clickToHide()
					},
					fail: () => {},
					complete: () => {}
				});
			},
			show(){
				this.$refs.shell.show()
			},
			clickSwtich() {
				this.isPwd = !this.isPwd
			},
			inputGetPwd(e) {
				// console.log(e);
				this.pwdValue = e.detail.value
			},
			inputGetUser(e) {
				// console.log(e);
				this.usrValue = e.detail.value
			},
			confirmLogin() {
				if (this.usrValue.length == 0 || this.pwdValue.length == 0) {
					uni.showToast({
						title: '输入用户名和密码',
						icon: 'none'
					});
					return
				}

				let url = this.$params.host + this.$params.action_login
				let data = {
					username: this.usrValue,
					password: this.pwdValue

				}
				this.$requset.post(url, data, res => {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					if (!res.success) {
						this.$refs.shell.show()
						this.usrValue = ""
						this.pwdValue = ""
						return
					}
					// console.log(res);
					// console.log(data);
					// console.log(url);
					uni.getStorageSync('userLoginInfo', data)
					let app = getApp()
					app.globalData.uid = res.data.id
					app.globalData.token = res.data.token
					app.globalData.avatar = res.data.avatar
					app.globalData.type = res.data.type
					app.globalData.nickName = res.data.nickName
					app.globalData.userName = res.data.userName
					app.globalData.email = res.data.email
					this.$email("after")
					this.$noti.post(this.$params.noti_login_status)
				}, () => {})

			}

		}
	}
</script>

<style>
	@import url("login-dialog.css");
</style>
